package com.taxi.driver.module.main.mine.wallet.bill;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.qianxx.view.xrecyclerview.IXRecyclerViewListener;
import com.qianxx.view.xrecyclerview.XRecyclerView;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.module.main.mine.wallet.bill.dagger.BillModule;
import com.taxi.driver.module.main.mine.wallet.bill.dagger.DaggerBillComponent;
import com.taxi.driver.module.vo.BillVO;
import com.taxi.driver.widget.SwitchTable;
import com.taxi.driver.widget.TextViewPlus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 账单
 */
public class BillActivity extends BaseActivity implements BillContract.View, SwitchTable.DateListener {

    @BindView(R.id.recycler_view)
    XRecyclerView mXRecyclerView;

    private TabLayout mTab;
    private TextViewPlus mTvSwitch;
    private SwitchTable mSwitchTable;

    //时间模式
    private final int MONTH = 0;
    private final int WEEK = 1;

    //类型模式
    private final String ALL = "0";
    private final String INCOME = "1";
    private final String SPENDING = "2";

    private int dateType = MONTH;
    private String start;
    private String end;
    private String type = ALL;

    @Inject
    BillPresenter mPresenter;

    private BillRecordAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        ButterKnife.bind(this);

        DaggerBillComponent.builder()
                .appComponent(Application.getAppComponent())
                .billModule(new BillModule(this))
                .build().inject(this);

        initView();

        start = mSwitchTable.getMonth();
        end = start;

        initListener();
    }

    private void initView() {
        mAdapter = new BillRecordAdapter(this);
        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mXRecyclerView.setAdapter(mAdapter);

        View headView = LayoutInflater.from(this).inflate(R.layout.bill_header, mXRecyclerView, false);
        mTab = (TabLayout) headView.findViewById(R.id.tab);
        mTvSwitch = (TextViewPlus) headView.findViewById(R.id.tv_switch);
        mSwitchTable = (SwitchTable) headView.findViewById(R.id.switch_table);
        mAdapter.addHeaderView(headView);

        initHead();
    }

    private void initHead() {
        mTvSwitch.setOnClickListener(v -> {
            if (dateType == MONTH) {
                dateType = WEEK;
                mTvSwitch.setText(getResources().getString(R.string.switch_month));
                mSwitchTable.switchMode(mSwitchTable.WEEK);
            } else {
                dateType = MONTH;
                mTvSwitch.setText(getResources().getString(R.string.switch_week));
                mSwitchTable.switchMode(mSwitchTable.MONTH);
            }
        });

        mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTab.addTab(mTab.newTab().setText(getResources().getString(R.string.all)));
        mTab.addTab(mTab.newTab().setText(getResources().getString(R.string.income)));
        mTab.addTab(mTab.newTab().setText(getResources().getString(R.string.spending)));
        mTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        type = ALL;
                        break;
                    case 1:
                        type = INCOME;
                        break;
                    case 2:
                        type = SPENDING;
                        break;
                }
                mXRecyclerView.onRefresh();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mSwitchTable.setDateListener(this);
    }

    private void initListener() {
        mXRecyclerView.setXRecyclerViewListener(new IXRecyclerViewListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh(dateType, start, end, type);
            }

            @Override
            public void onLoadMore() {
                mPresenter.loadMore(dateType, start, end, type);
            }
        });

        mXRecyclerView.onRefresh();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void onMonth(TextView fee, String month) {
        dateType = MONTH;
        start = month;
        end = start;
        mXRecyclerView.onRefresh();
    }

    @Override
    public void onWeek(TextView fee, String startDay, String endDay) {
        dateType = WEEK;
        start = startDay;
        end = endDay;
        mXRecyclerView.onRefresh();
    }

    @Override
    public void setList(BillVO vo) {
        mXRecyclerView.refreshComplete();
        if (!TextUtils.isEmpty(vo.total)) {
            mSwitchTable.getTitleView().setText(vo.total);
        } else {
            mSwitchTable.getTitleView().setText("0.0");
        }
        if (vo.bills.size() <= 0) {
            mXRecyclerView.setLoadHide();
        } else if (vo.bills.size() < 10) {
            mXRecyclerView.setLoadComplete(true);
        }
        mAdapter.setAll(vo.bills);
    }

    @Override
    public void appendList(BillVO vo) {
        if (vo.bills.size() < 10) {
            mXRecyclerView.setLoadHide();
        } else {
            mXRecyclerView.setLoadComplete(false);
        }
        mAdapter.addAll(vo.bills);
    }
}
